import StringHelper from "../tools/StringHelper";
import ResItem from "../res/ResItem";
import PoolManager from "./PoolManager";
import ResLoader from "../res/ResLoader";
import ResDir from "../res/ResDir";

export class PoolConfig{
    key:string;
    type: typeof cc.Asset;
    count: number;
}
export default class PoolResManager  {
    private loader: ResLoader;
    constructor(loader: ResLoader) {
        this.loader = loader;
    }
    // //对象池表
    private pools = {};
    loadArray(list:PoolConfig[]) {
        for (let index = 0; index < list.length; index++) {
            let element:PoolConfig = list[index];
            this.loadRes(element.key,element.type, element.count)
        }
    }
    loadDir(path, count:number, func?: () => void){
           this.loader.loadDir(path, (err, item: ResDir) => {
            if (err) {
                return;
            }
            // let key = StringHelper.laststring(path,'/')
            PoolManager.instance().init(path,item,count);
            this.pools[path] = true;
            if (func) {
                func();
            }

        });    
    }
    /**
     * 通过一个目录下的资源初始化node
     * @param path 
     * @param func 
     * @param count 
     */
    getByDir(path: string, func: (node:  cc.Node[]) => void, count: number = 1){
        // let key = StringHelper.laststring(path,'/')      
        if (!this.pools[path]) {
            this.loadDir(path,count, () => {
                let list = []
                for (let index = 0; index < count; index++) {
                    list.push(this.getNode(path))
                }
                func(list)

            });
        } else {
            let list = []
            for (let index = 0; index < count; index++) {
                list.push(this.getNode(path))
            }
            func(list)
        }
    }

    loadRes(path: string, type: typeof cc.Asset, count:number, func?: () => void) {
        this.loader.loadRes(path, type, (err, item: ResItem) => {
            if (err) {
                return;
            }
            // let key = StringHelper.laststring(path,'/')
            PoolManager.instance().init(path,item,count*2);
            this.pools[path] = true;
            if (func) {
                func();
            }

        });
    }

    /**
     * 获得被销毁的对象
     * @param name 
     */
    get(path: string, type: typeof cc.Asset, func: (node:  cc.Node[]) => void, count: number = 1) {
        // let key = StringHelper.laststring(path,'/')      
        if (!this.pools[path]) {
            this.loadRes(path, type,count, () => {
                let list = []
                for (let index = 0; index < count; index++) {
                    list.push(this.getNode(path))
                }
                func(list)

            });
        } else {
            let list = []
            for (let index = 0; index < count; index++) {
                list.push(this.getNode(path))
            }
            func(list)
        }
    } 

    // getPool(path:string){
    //     return this.pools[path];
    // }

    private getNode(key: string): cc.Node {
        // cc.log("getNode --------------------------- 对象池剩余对象数量  ",key,this.pools[key].size());
            // let go: cc.Node = this.pools[key].get();
            // if (!this.nameMap[go.name] && go.name != key) {
            //     this.nameMap[go.name] = key;
            // }
        return PoolManager.instance().get(key);
    }


    put(go: cc.Node) {

        PoolManager.instance().put(go)
    }

    // clear(name: string) {
    //     PoolManager.instance().clear(name)
    //     this.pools[name] = null;
    // }
    // clealAll() {
    //     PoolManager.instance().clealAll()
    //     this.pools = {};
    // }
}
